package com.woparker.kiosk.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

public class MyDialog extends Dialog{

    public MyDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public void createDialog(Context context, View contentView,
                             boolean isCancelable, ViewGroup.LayoutParams params){
        this.setContentView(contentView, params);
        this.setCanceledOnTouchOutside(isCancelable);
        this.setCancelable(isCancelable);
        if (true == isCancelable) {
            this.setOnKeyListener(keylistener);
        } else {
            this.setOnKeyListener(keyDisablelistener);
        }
        if (!this.isShowing())
            this.show();
    }

    public OnKeyListener keylistener = new OnKeyListener() {

        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)

            {
                if (dialog != null) {
                    dialog.cancel();
                }
                return true;
            } else {
                return false;

            }
        }
    };

    public OnKeyListener keyDisablelistener = new OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                return true;
            } else {
                return false;
            }
        }
    };

    @Override
    public boolean dispatchTouchEvent(@NonNull MotionEvent event) {
        return super.dispatchTouchEvent(event);
    }
}
